﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Acme.BookStore.Migrations
{
    /// <inheritdoc />
    public partial class UpdateSorehouseModels : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            // 首先添加一个临时列
            migrationBuilder.AddColumn<int>(
                name: "Sort_New",
                table: "SorehouseModels",
                type: "integer",
                nullable: false,
                defaultValue: 0);

            // 更新临时列，将可以转换的数据转换为整数
            migrationBuilder.Sql(@"
                UPDATE ""SorehouseModels"" 
                SET ""Sort_New"" = CASE 
                    WHEN ""Sort"" ~ '^[0-9]+$' THEN ""Sort""::integer 
                    ELSE 0 
                END;");

            // 删除原来的 Sort 列
            migrationBuilder.DropColumn(
                name: "Sort",
                table: "SorehouseModels");

            // 重命名临时列为 Sort
            migrationBuilder.RenameColumn(
                name: "Sort_New",
                table: "SorehouseModels",
                newName: "Sort");

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseStatus",
                table: "SorehouseModels",
                type: "character varying(50)",
                maxLength: 50,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseName",
                table: "SorehouseModels",
                type: "character varying(100)",
                maxLength: 100,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseManager",
                table: "SorehouseModels",
                type: "character varying(100)",
                maxLength: 100,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseCode",
                table: "SorehouseModels",
                type: "character varying(50)",
                maxLength: 50,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseAddress",
                table: "SorehouseModels",
                type: "character varying(200)",
                maxLength: 200,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "Remarks",
                table: "SorehouseModels",
                type: "character varying(500)",
                maxLength: 500,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "Phone",
                table: "SorehouseModels",
                type: "character varying(20)",
                maxLength: 20,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.AlterColumn<string>(
                name: "ManagementDept",
                table: "SorehouseModels",
                type: "character varying(100)",
                maxLength: 100,
                nullable: false,
                oldClrType: typeof(string),
                oldType: "text");

            migrationBuilder.CreateTable(
                name: "locationModels",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "uuid", nullable: false),
                    LocationId = table.Column<string>(type: "text", nullable: false),
                    LocationName = table.Column<string>(type: "text", nullable: false),
                    Capacity = table.Column<decimal>(type: "numeric", nullable: false),
                    Supervisor = table.Column<string>(type: "text", nullable: false),
                    Status = table.Column<int>(type: "integer", nullable: false),
                    SortOrder = table.Column<int>(type: "integer", nullable: false),
                    Remarks = table.Column<string>(type: "text", nullable: false),
                    WarehouseId = table.Column<Guid>(type: "uuid", nullable: false),
                    ExtraProperties = table.Column<string>(type: "text", nullable: false),
                    ConcurrencyStamp = table.Column<string>(type: "character varying(40)", maxLength: 40, nullable: false),
                    CreationTime = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
                    CreatorId = table.Column<Guid>(type: "uuid", nullable: true),
                    LastModificationTime = table.Column<DateTime>(type: "timestamp without time zone", nullable: true),
                    LastModifierId = table.Column<Guid>(type: "uuid", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_locationModels", x => x.Id);
                });
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "locationModels");

            // 为了回滚，我们需要先创建一个临时的文本列
            migrationBuilder.AddColumn<string>(
                name: "Sort_Old",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                defaultValue: "0");

            // 将整数值转换为字符串
            migrationBuilder.Sql(@"
                UPDATE ""SorehouseModels"" 
                SET ""Sort_Old"" = ""Sort""::text;");

            // 删除整数列
            migrationBuilder.DropColumn(
                name: "Sort",
                table: "SorehouseModels");

            // 重命名回原来的列名
            migrationBuilder.RenameColumn(
                name: "Sort_Old",
                table: "SorehouseModels",
                newName: "Sort");

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseStatus",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(50)",
                oldMaxLength: 50);

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseName",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(100)",
                oldMaxLength: 100);

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseManager",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(100)",
                oldMaxLength: 100);

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseCode",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(50)",
                oldMaxLength: 50);

            migrationBuilder.AlterColumn<string>(
                name: "WarehouseAddress",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(200)",
                oldMaxLength: 200);

            migrationBuilder.AlterColumn<string>(
                name: "Remarks",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(500)",
                oldMaxLength: 500);

            migrationBuilder.AlterColumn<string>(
                name: "Phone",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(20)",
                oldMaxLength: 20);

            migrationBuilder.AlterColumn<string>(
                name: "ManagementDept",
                table: "SorehouseModels",
                type: "text",
                nullable: false,
                oldClrType: typeof(string),
                oldType: "character varying(100)",
                oldMaxLength: 100);
        }
    }
}
